package cn.tsvtc.health.controller;


import cn.tsvtc.health.common.constant.MessageConstant;
import cn.tsvtc.health.common.entity.CommonResult;
import cn.tsvtc.health.common.entity.PageResult;
import cn.tsvtc.health.common.entity.QueryPageBean;
import cn.tsvtc.health.pojo.CheckItem;
import cn.tsvtc.health.service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Athena
 * @date: 2020/11/16 16:30
 * @description:
 */
@RequestMapping("checkitem")
@RestController
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //查询分页的list
    @RequestMapping("page/list")
    @ResponseBody
    public CommonResult getList(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = checkItemService.findAll(queryPageBean);
            return new CommonResult(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.COMMON_ERROR);
        }
    }

    @RequestMapping("page/listAll")
    @ResponseBody
    public CommonResult getAll(){
        try{
            List<CheckItem> all = checkItemService.getAll();
            return new CommonResult(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, all);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.COMMON_ERROR);
        }
    }



    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommonResult pageList(@PathVariable Integer id){
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return new CommonResult(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.QUERY_CHECKITEM_FAIL, MessageConstant.COMMON_ERROR);
        }
    }

    @GetMapping("del/{id}")
    @ResponseBody
    public CommonResult del(@PathVariable Integer id){
        try {
            int delete = checkItemService.delete(id);
            return new CommonResult(true, MessageConstant.DELETE_CHECKITEM_SUCCESS, delete);
        }catch (Exception e) {
            return new CommonResult(false, MessageConstant.DELETE_CHECKITEM_FAIL, e.getMessage());
        }
    }

    @PostMapping("update")
    @ResponseBody
    public CommonResult update(@RequestBody CheckItem checkitem){

        int update = 0;
        try {
            update = checkItemService.update(checkitem);
            return new CommonResult(true, MessageConstant.EDIT_CHECKITEM_SUCCESS, update);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(false, MessageConstant.EDIT_CHECKITEM_FAIL, e.getMessage());
        }
    }

    @PostMapping("insert")
    @ResponseBody
    public CommonResult create(@RequestBody CheckItem checkitem){
        int insert = 0;
        try {
            insert = checkItemService.insert(checkitem);
            return new CommonResult(true, MessageConstant.ADD_CHECKITEM_SUCCESS, insert);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(false, MessageConstant.ADD_CHECKITEM_FAIL, e.getMessage());
        }
    }

}
